package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.interactive.InteractiveRecord;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

public class InteractiveRecordDaoImpl extends TempDataObjectDao<InteractiveRecord> {

    public LinkedList<InteractiveRecord> getInteractiveRecordList(long userId, int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<InteractiveRecord> dataList = new LinkedList<>();
        String sql = "select * from t_u_interactive_record where userId=? order by recordTime desc limit " + maxCount;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                InteractiveRecord data = parse(rs);
                dataList.addLast(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(InteractiveRecord data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_interactive_record(userId,recordUid,interactiveType,operateUserId,operateBaseInfo,recordTime) values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getRecordUid());
            call.setInt(i++, data.getInteractiveType());
            call.setLong(i++, data.getOperateUserId());
            call.setString(i++, data.getOperateBaseInfoJsonStr());
            call.setLong(i++, data.getRecordTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(InteractiveRecord data) {
        PreparedStatement call = null;
        String sql = "update t_u_interactive_record set userId=?,interactiveType=?,operateUserId=?,operateBaseInfo=?,recordTime=? where recordUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getInteractiveType());
            call.setLong(i++, data.getOperateUserId());
            call.setString(i++, data.getOperateBaseInfoJsonStr());
            call.setLong(i++, data.getRecordTime());
            call.setString(i++, data.getRecordUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected InteractiveRecord parseData(ResultSet rs) throws SQLException {
        InteractiveRecord data = new InteractiveRecord();
        data.setUserId(rs.getLong("userId"));
        data.setRecordUid(rs.getString("recordUid"));
        data.setInteractiveType(rs.getInt("interactiveType"));
        data.setOperateUserId(rs.getLong("operateUserId"));
        String baseUserInfoStr = rs.getString("operateBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setOperateBaseInfo(userBaseInfo);
        }
        data.setRecordTime(rs.getLong("recordTime"));
        return data;
    }
}
